package com.System.LearnSystemStudent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.System.LearnSystem.Course;
import com.System.LearnSystem.Result;
import com.System.LearnSystem.Student;
import com.System.LearnSystem.Token;
import com.System.LearnSystem.sqlConnection;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

class Message  //用于临时储存客户端发来的请求信息
{
	protected String token;	
	protected String student_no;
	protected String role;
	
	public String gettoken()
	{
		return token;
	}
	public void settoken(String token)
	{
		this.token = token;
	}
	
	public String getstudent_no()
	{
		return student_no;
	}
	public void setstudent_no(String student_no)
	{
		this.student_no = student_no;
	}
	
	public String getrole()
	{
		return role;
	}
	public void setrole(String role)
	{
		this.role = role;
	}
}


@WebServlet("/stu/searchmine")
public class searchMine extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException
	{
		StringBuilder json = new StringBuilder();//读取json请求
        BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            json.append(line);
        }
        String jsonStr = json.toString();     
        ObjectMapper objectMapper = new ObjectMapper();
        Message message = objectMapper.readValue(jsonStr, Message.class);
        
        objectMapper.registerModule(new JavaTimeModule());//注册驱动保证序列化
        res.setContentType("application/json;charset=UTF-8");//设置响应格式
        PrintWriter out = res.getWriter();
        if(message.gettoken()!=null)
        {
        	if(Token.checkToken(message.gettoken())&&"STUDENT".equals(message.getrole()))//验证令牌和身份
        	{
        		String sql = "SELECT * FROM t_student WHERE  student_no = ?";//预处理语句
        		try (Connection conn = sqlConnection.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql))
        		{
        			pstmt.setString(1,message.getstudent_no());//将对应信息替换？的位置
        			ResultSet rs = pstmt.executeQuery();
        			Student student = new Student();
        			while(rs.next())
        			{
        				student.setId(rs.getLong("id"));
        				student.setStudentNo(rs.getString("student_no"));
        				student.setUsername(rs.getString("username"));
        				student.setDepartmentId(rs.getLong("department_id"));//若时间还够就编写详细的院系信息表
        				student.setName(rs.getString("name"));
        				student.setEmail(rs.getString("email"));
        				student.setPhone(rs.getString("phone"));
        				student.setRole("STUDENT");        				
	        			Result<Student> result = Result.success(student);
	        			out.println(objectMapper.writeValueAsString(result));
        			}
        			if(student.getStudentNo()==null)
        			{
        				Result<Object> result = Result.error(403,"未查询到该学生");
                		out.println(objectMapper.writeValueAsString(result));
        			}
        			
        		} catch (SQLException e) {
        			System.err.println("SQL 错误码: " + e.getErrorCode());
          		  	System.err.println("SQL 错误消息: " + e.getMessage());
          		    e.printStackTrace();
				}
        	}
        	else
        	{
        		Result<Object> result = Result.error(401,"非授权访问");
        		out.println(objectMapper.writeValueAsString(result));
        	}
        }
        else
        {
        	Result<Object> result = Result.error(401,"非授权访问");
        	out.println(objectMapper.writeValueAsString(result));
        }
	}
}
